package cn.edu.cqvie.netty.grpc;

import cn.edu.cqvie.netty.proto.*;
import io.grpc.stub.StreamObserver;

import java.util.UUID;

public class StudentServiceImpl extends StudentServiceGrpc.StudentServiceImplBase {

    @Override
    public void getRealNameByUsername(MyRequest request, StreamObserver<MyRespone> responseObserver) {
        //打印参数
        System.out.println("接受客户端信息：" + request.getUsername());

        //返回结果
        responseObserver.onNext(MyRespone.newBuilder().setRealname("张三").build());
        //标志结束
        responseObserver.onCompleted();
    }


    @Override
    public void getStudentsByAge(StudentRequest request, StreamObserver<StudentRespone> responseObserver) {
        //打印参数
        System.out.println("接受客户端信息：" + request.getAge());

        //返回结果
        responseObserver.onNext(StudentRespone.newBuilder().setName("张三").setAge(20).setCity("北京").build());
        responseObserver.onNext(StudentRespone.newBuilder().setName("李四").setAge(21).setCity("天津").build());
        responseObserver.onNext(StudentRespone.newBuilder().setName("王五").setAge(22).setCity("成都").build());
        responseObserver.onNext(StudentRespone.newBuilder().setName("赵六").setAge(23).setCity("深圳").build());

        //标志结束
        responseObserver.onCompleted();
    }

    @Override
    public StreamObserver<StudentRequest> getStudentsWrapperByAge(StreamObserver<StudentResponeList> responseObserver) {
        return new StreamObserver<StudentRequest>() {

            @Override
            public void onNext(StudentRequest value) {
                System.out.println("onNext: " + value.getAge());
            }

            @Override
            public void onError(Throwable t) {

            }

            @Override
            public void onCompleted() {
                StudentRespone respone1 = StudentRespone.newBuilder().setName("张三").setAge(20).setCity("北京").build();
                StudentRespone respone2 = StudentRespone.newBuilder().setName("李四").setAge(21).setCity("天津").build();

                StudentResponeList studentResponeList = StudentResponeList.newBuilder()
                        .addStudentRespone(respone1)
                        .addStudentRespone(respone2)
                        .build();

                responseObserver.onNext(studentResponeList);
                responseObserver.onCompleted();
            }
        };
    }

    @Override
    public StreamObserver<StreamRequest> biTalk(StreamObserver<StreamRespone> responseObserver) {
        return new StreamObserver<StreamRequest>() {

            @Override
            public void onNext(StreamRequest value) {
                System.out.println(value.getRequestInfo());

                responseObserver.onNext(StreamRespone.newBuilder().setResponeInfo(UUID.randomUUID().toString()).build());
            }

            @Override
            public void onError(Throwable t) {
                t.printStackTrace();
            }

            @Override
            public void onCompleted() {

                responseObserver.onCompleted();
            }
        };
    }
}
